<template>
  <c-form-dialog
    ref="form"
    :visible.sync="innerVisible"
    :form-items="formItems"
    :form-data="formData"
    :table-name="tableName"
    :table-id="tableId"
    @close="handleClose"
    @success="handleSuccess"
    :before-save="handleBeforeSave"
  ></c-form-dialog>
</template>

<script>
import { license18Validate } from '@vimi/utils-tool'
import { idCardValidator, mobileValidator } from '@/common/plugins/validator'
import SelectAddress from '@/components/select/SelectAddress'
export default {
  components: {},
  props: {
    visible: Boolean,
    tableId: String,
    formData: {
      type: Object,
      default() {
        return {}
      },
    },
  },
  data() {
    const registerNoValidator = (rule, value, callback) => {
      if (value === '') {
        callback()
      } else if (!license18Validate(value)) {
        callback(new Error('错误的营业执照号，请重新输入'))
      } else {
        callback()
      }
    }
    return {
      innerVisible: false,
      tableName: 'customer_related',
      formItems: [
        {
          tag: 'input',
          itemAttrs: {
            label: '主体名称',
            rules: [{ required: true, message: '请输入主体名称', trigger: 'blur' }],
          },
          attrs: { key: 'customer_name', value: '', placeholder: '请输入主体名称' },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '营业执照号',
            rules: [
              { required: true, message: '请输入营业执照号', trigger: 'blur' },
              {
                validator: registerNoValidator,
                message: '请输入正确的营业执照号',
                trigger: 'blur',
              },
            ],
          },
          attrs: { key: 'register_no', value: '', placeholder: '请输入信息' },
        },
        {
          tag: SelectAddress,
          itemAttrs: {
            label: '经营地址',
            rules: { required: true, message: '请选择经营地址', trigger: 'change' },
          },
          attrs: {
            key: 'address_code',
            value: '',
            placeholder: '请选择经营地址',
            class: 'form-control-ml',
            autoTop: true,
          },
        },
        {
          tag: 'input',
          itemAttrs: { label: '详细地址' },
          attrs: { key: 'address_detail', value: '', placeholder: '请输入详细地址（非必填）' },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '经营者姓名',
            rules: [{ required: true, message: '请输入经营者姓名', trigger: 'blur' }],
          },
          attrs: { key: 'corp_name', value: '', placeholder: '经营者姓名' },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '身份证号码',
            rules: [
              { required: true, message: '请输入身份证号码', trigger: 'blur' },
              { validator: idCardValidator, message: '请输入正确的身份证号码', trigger: 'blur' },
            ],
          },
          attrs: { key: 'lender_idcard', value: '', placeholder: '请输入身份证号码' },
        },
        {
          tag: 'input',
          itemAttrs: {
            label: '联系电话',
            rules: [
              { required: true, message: '请输入手机号码', trigger: 'blur' },
              { validator: mobileValidator, message: '请输入正确的手机号码', trigger: 'blur' },
            ],
          },
          attrs: { key: 'mobile_no', value: '', placeholder: '联系电话' },
        },
        {
          tag: 'input',
          itemAttrs: { label: '身份证地址' },
          attrs: { key: 'cert_address', value: '', placeholder: '请输入身份证地址' },
        },
      ],
    }
  },
  computed: {},
  watch: {
    visible(val) {
      this.innerVisible = val
    },
  },
  created() {},
  mounted() {},
  methods: {
    handleBeforeSave(model) {
      return model
    },
    handleClose() {
      this.$emit('update:visible', false)
    },
    handleSuccess(res) {
      this.$emit('success', res)
    },
  },
}
</script>
<style lang="scss" scoped></style>
